

const path = require("path");
const htmlwebpackplugin = require("html-webpack-plugin");
const { VueLoaderPlugin } = require("vue-loader");
const { merge } = require("webpack-merge");
// const glob = require('glob');
// const {PurgeCSSPlugin} = require('purgecss-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const TerserPlugin = require('terser-webpack-plugin');
const base = {
    output: {
        path: path.join(__dirname, "dist"),
        filename:"main.js",
        libraryTarget: 'umd',
        clean:true
    },
    resolve: {
        alias: {
            "src": path.join(__dirname, "./packages/src"),
            "packages": path.join(__dirname, "./packages"),
            "handers": path.join(__dirname, "./handers")
        },
        extensions: ['.js', '.jsx', '.vue']
    },
    module: {
        rules: [
            {
                test: /\.vue$/,
                use: [
                    "vue-loader"
                ]
            },
            {
                test: /\.js$/,
                use: "babel-loader"
            },
            {
                test: /\.css$/,
                use: [MiniCssExtractPlugin.loader, "css-loader"]
            }
        ]
    },
    plugins: [
        new VueLoaderPlugin(),new MiniCssExtractPlugin()
    ],
}
const developmentConfig = {
    entry: "./main.js",
    plugins: [new htmlwebpackplugin({
        template: "./public/index.html"
    })],
    mode: "development",
    devServer: {
        hot: true,
        host: "localhost",
        port: 8090,
    }
}
const productionConfig = {
    entry: "./packages/index.js",
    mode: "production",
    optimization: {
        minimize: true,
        minimizer: [new TerserPlugin({
            extractComments:false
        })],
    },
    plugins: [
        // new PurgeCSSPlugin({
        //     paths:glob.sync(path.join(__dirname,'./public/*.html'))//src下所有的html
        // }),
        new MiniCssExtractPlugin()
    ],
}
const config = merge(base, process.env.NODE_ENV === 'development'?  developmentConfig :productionConfig);


module.exports = config
